import { defineStore } from "pinia";
import { useRequest } from "@/hooks";
import { APIS } from "@/service";

export * from "./modules";

/**
 * @description 宠物仓库类型定义
 */
type PetStoreType = {
    current: string; // 当前选中的宠物 ID
    list: Pet[]; // 宠物列表
    add: {
        step: number;
        pet: Pet;
    };
};

/**
 * @description 宠物类型枚举
 */
type PetType =
    | "dog"
    | "cat"
    | "bird"
    | "fish"
    | "reptile"
    | "small-animal" // 仓鼠/兔子等
    | "other";

/**
 * @description 宠物信息接口定义
 */
export type Pet = {
    id?: string; // 唯一标识
    name?: string; // 宠物名字
    type?: PetType; // 宠物类型
    breed?: string; // 品种(可选)
    age?: number; // 年龄
    remarks?: string; // 备注
    ageUnit?: "days" | "weeks" | "months" | "years"; // 年龄单位
    gender?: "male" | "female" | "unknown"; // 性别
    weight?: number | string; // 体重
    weightUnit?: "kg" | "g" | "lb"; // 体重单位
    color?: string; // 毛色
    birthDate?: Date | string; // 出生日期
    avatar?: string; // 头像URL
    microchipNumber?: string; // 芯片编号
    isSterilized?: boolean; // 是否绝育
    description?: string; // 描述/个性
};

const defaultAddPet: Pet = {
    type: "dog",
    breed: "",
    ageUnit: "years",
    weight: "",
    weightUnit: "kg",
    color: "",
    isSterilized: false
};

/**
 * @description 定义宠物状态管理仓库
 * @returns {Object} 返回一个 Pinia 状态管理对象
 */
export const usePetStore = defineStore("pet", {
    state: (): PetStoreType => ({
        current: "01", // 默认当前选中宠物 ID
        /**
         * @description 初始化宠物列表数据
         */
        list: [],
        add: {
            step: 0,
            pet: defaultAddPet
        }
    }),
    actions: {
        /**
         * @description 更新当前选中的宠物
         * @param {Pet} payload - 新的宠物对象
         */
        onUpdatePet(payload: Pet) {
            if (payload.id) this.current = payload.id;
        },

        /**
         * @description 更新整个宠物列表
         */
        onUpdatePetsList() {
            /**
             * @description 用户详情请求实例
             * */
            useRequest(APIS.PET_LIST, "POST", {
                retryCount: 3,
                retryDelay: 5000,
                cacheTime: 500,
                onSuccess: ({ data }) => {
                    this.list = [
                        {
                            id: "01",
                            name: "小旺",
                            type: "dog",
                            breed: "秋吉",
                            age: 2,
                            ageUnit: "years",
                            gender: "male",
                            weight: 5,
                            weightUnit: "kg",
                            color: "米色",
                            birthDate: "2022-01-01",
                            avatar: "/static/images/pets/01.png",
                            microchipNumber: "1234567890",
                        }
                    ];
                },
                onError: (error) => {
                    uni.showToast({
                        icon: "none",
                        title: error.errMsg
                    });
                }
            }).run();
        },

        updateCurrentStep(step: number) {
            this.add.step = step;
        },

        clearAddPetDetail() {
            this.updateCurrentStep(0);
            this.add.pet = {};
        },

        updateAddPetDetail(payload: Pet) {
            Object.keys(payload).forEach(key => {
                console.log(key);
            });
        }
    },
    getters: {
        /**
         * @description 获取所有宠物列表
         * @returns {Pet[]} 返回宠物数组
         */
        pets(): Pet[] {
            return this.list;
        },

        currentStep(): number {
            return this.add.step;
        },

        stepCount(): number {
            return 4;
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: import.meta.env.MODE + "-pet-store"
            }
        ]
    }
});
